<?php

namespace app\common\service;

use tech\core\eSign;
use app\common\model\ContractModel;
use app\common\model\ContractTemplateModel;

/**
 * pdf填充-e签宝
 *
 * @author lcx
 */
class PdfService {

	/**
	 * 调用PdfService::fillPdf(7);
	 * pdf填充--将数据填充到PDF模板
	 * @param type $contract_id 合同ID
	 */
	public static function fillPdf($contract_id) {
		//初始化合同
		try {
			require_once EXTEND_PATH . '/eSign/API/eSignOpenAPI.php';
			$sign = new eSign();
			$iRet = $sign->init();
			if ($iRet != 0) {
				file_put_contents('./contract_fill_pdf.txt', date('Y-m-d H:i:s') . '合同ID:' . $contract_id . '服务初始化失败' . PHP_EOL, FILE_APPEND | LOCK_EX); //解绑失败写入文件日志
				return false;
			}
		} catch (\Exception $e) {
			file_put_contents('./contract_fill_pdf.txt', date('Y-m-d H:i:s') . '合同ID:' . $contract_id . $e->getMessage() . PHP_EOL, FILE_APPEND | LOCK_EX); //解绑失败写入文件日志
			return fasle;
		}
		//查询合同信息
		$contract_model = new ContractModel();
		$contract_template_model = new ContractTemplateModel();
		$contract_info = $contract_model->getOne(['contract_id' => $contract_id], [], 'template_no,contract_no,parent_chinese_name,parent_english_name,parent_passport_no,parent_birthday,donor_no,pregnant_mother_no');
		if (empty($contract_info)) {
			file_put_contents('./contract_fill_pdf.txt', date('Y-m-d H:i:s') . '合同ID:' . $contract_id . '合同信息错误' . PHP_EOL, FILE_APPEND | LOCK_EX); //解绑失败写入文件日志
			return false;
		}
		$template_info = $contract_template_model->getOne(['template_no' => $contract_info['template_no'], 'status' => 1], 'local_path,local_path_en');
		file_exists('./pdf/PDF_Generate/') || mkdir('./pdf/PDF_Generate/', 0777);
		$pdf_path = './pdf/PDF_Generate/';
		//合同模板组装
		$arr = [0 => ['local_path' => $template_info['local_path'], 'lang' => 1], 1 => ['local_path' => $template_info['local_path_en'], 'lang' => 2]];
		foreach ($arr as $k => $v) {
			if (empty($v['local_path'])) {
				continue;
			}
			$pdf_file = date('YmdHis') . '-' . $contract_info['contract_no'] . '-' . $contract_info['template_no'] . '-' . $v['lang'] . '.pdf';
			$temp_pdf = $v['local_path'];
			$tmpFile = array(
				'srcFileUrl' => ROOT_PATH . '/public/pdf/PDF_Template/' . $temp_pdf,
				'dstFileUrl' => ROOT_PATH . '/public/' . $pdf_path . $pdf_file
			);
			if ($v['lang'] == 1) {
				$txtFields = [
					'contract_no' => $contract_info['contract_no'],
					'parent_name' => $contract_info['parent_chinese_name'], //准父母姓名
					'parent_passport_no' => $contract_info['parent_passport_no'], //准父母护照号,
					'parent_birthday' => date('Y年m月d日', $contract_info['parent_birthday']), //出生日期
					'donor_no' => $contract_info['donor_no'], //捐赠者编号
					'pregnant_mother_no' => $contract_info['pregnant_mother_no'], //孕母编号
				];
			} else {
				$txtFields = [
					'contract_no' => $contract_info['contract_no'],
					'parent_name' => $contract_info['parent_english_name'], //准父母姓名
					'parent_passport_no' => $contract_info['parent_passport_no'], //准父母护照号,
					'parent_birthday' => date('Y年m月d日', $contract_info['parent_birthday']), //出生日期
					'donor_no' => $contract_info['donor_no'], //捐赠者编号
					'pregnant_mother_no' => $contract_info['pregnant_mother_no'], //孕母编号
					'parent_parent_birthday_d' => date('d', $contract_info['parent_birthday']),
					'parent_parent_birthday_m' => date('m', $contract_info['parent_birthday']),
					'parent_parent_birthday_y' => substr(date('Y', $contract_info['parent_birthday']), -1, 2),
				];
			}
			$data = $sign->createFromTemplate($tmpFile, false, $txtFields, false);
			if ($data['errCode'] == 0) {
				//保存文件名称
				if ($v['lang'] == 1) {
					$edit['generate_contract_zn'] = $pdf_file;
				} else {
					$edit['generate_contract_en'] = $pdf_file;
				}
				$res = $contract_model->edit(['contract_id' => $contract_id], $edit);
				if (!$res) {
					file_put_contents('./contract_fill_pdf.txt', date('Y-m-d H:i:s') . '合同ID:' . $contract_id . 'PDF填充成功但保存失败' . PHP_EOL, FILE_APPEND | LOCK_EX); //解绑失败写入文件日志
					return false;
				}
			}
		}
		return true;
	}

}
